import 'package:kkhousekeep/models/order_list_entity.dart';
import 'package:kkhousekeep/models/property_order_detail_entity.dart';
import 'package:kkhousekeep/models/repair_department_list_entity.dart';
import 'package:kkhousekeep/models/repair_staff_list_entity.dart';
import 'package:kkhousekeep/models/repair_standard_list_entity.dart';
import 'package:kkhousekeep/net/api.dart';
import 'package:kkhousekeep/net/respnse_code.dart';
import 'package:kkhousekeep/tools/dialog_util.dart';
import 'package:kkhousekeep/tools/log.dart';

import '../models/base_respond_entity.dart';
import '../models/order_count_entity.dart';
import '../models/order_result_entity.dart';
import 'base_connect.dart';

class HomeOrderConnect extends BaseConnect {
  Future<RepairStandardListEntity> getOrderRepairStandardList({
    required int workOrderId,
  }) async {
    var response =
        await get("${Api.getRepairStandardList}?workOrderId=$workOrderId");
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value(
        RepairStandardListEntity()
            .copyWith(code: ResponseCode.timeout, obj: [], msg: ''),
      );
    }
    RepairStandardListEntity entity =
        RepairStandardListEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<OrderCountObj> getOrderCount({
    required int type,
  }) async {
    var response = await get(Api.getPropertyOrderCount, query: {
      'type': type == 0 ? 'self' : 'transferred',
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value(
        OrderCountObj()
            .copyWith(pending: 0, processed: 0, hanging: 0, processing: 0),
      );
    }
    OrderCountEntity entity = OrderCountEntity.fromJson(response.body);
    return Future.value(entity.obj);
  }

  Future<OrderListEntity> getOrderList({
    required String type,
    required String state,
    String repairArea = "0",
    String urgency = "0",
  }) async {
    ///state 待处理pending 处理中processing 挂单中registration 已处理processed
    var response = await get(Api.getPropertyWorkOrderList, query: {
      'type': type,
      'state': state,
      'area': repairArea,
      'urgency': urgency,
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return OrderListEntity()
          .copyWith(code: ResponseCode.timeout, obj: [], msg: "");
    }
    OrderListEntity entity = OrderListEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<BaseRespondEntity?> orderReceive({
    required int orderId,
  }) async {
    var response = await patch(Api.propertyOrderReceive, {}, query: {
      'workOrderId': orderId.toString(),
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    BaseRespondEntity entity = BaseRespondEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<BaseRespondEntity?> orderTransfer({
    required int orderId,
    required int repairDepartmentId,
    required int repairStaffId,
    required String mark,
  }) async {
    var response = await patch(Api.propertyOrderTransfer, {
      "workOrderId": orderId,
      "repairDepartmentId": repairDepartmentId,
      "repairStaffId": repairStaffId,
      "mark": mark
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    BaseRespondEntity entity = BaseRespondEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<BaseRespondEntity?> orderPending({
    required int orderId,
    required String explain,
  }) async {
    var response = await patch(Api.propertyOrderPending, {
      'workOrderId': orderId,
      'explain': explain,
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    BaseRespondEntity entity = BaseRespondEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<BaseRespondEntity?> orderCancelPending({
    required int orderId,
  }) async {
    var response = await patch(
        "${Api.propertyOrderCancelPending}?workOrderId=$orderId", {});
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    BaseRespondEntity entity = BaseRespondEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<BaseRespondEntity?> orderComplete({
    required int orderId,
    required int standardId,
    required String result,
    required String cause,
    required String propose,
    required String appraise,
    required double cost,
    required String repairStaffId,
    required List<String> pics,
  }) async {
    var response = await patch(Api.propertyOrderComplete, {
      'workOrderId': orderId,
      'standardId': standardId,
      'result': result,
      'cause': cause,
      'propose': propose,
      'appraise': appraise,
      'cost': cost,
      'repairStaffs': repairStaffId,
      'pics': pics,
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    logD({
      'workOrderId': orderId,
      'standardId': standardId,
      'result': result,
      'cause': cause,
      'propose': propose,
      'appraise': appraise,
      'cost': cost,
      'repairStaffs': repairStaffId,
      'pics': pics,
    }.toString());
    BaseRespondEntity entity = BaseRespondEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<PropertyOrderDetailEntity> getOrderDetail({
    required int orderId,
  }) async {
    var response = await get(Api.getPropertyOrderDetail, query: {
      'workOrderId': orderId.toString(),
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return PropertyOrderDetailEntity().copyWith(
        code: ResponseCode.timeout,
        obj: PropertyOrderDetailObj().copyWith(
            area: -1,
            address: "",
            reporter: "",
            phone: "",
            urgency: -1,
            fatalType: "",
            fatalDesc: "",
            pics: [],
            record: []),
      );
    }
    PropertyOrderDetailEntity entity =
        PropertyOrderDetailEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<OrderResultEntity> getOrderResult({
    required int orderId,
  }) async {
    var response = await get(Api.getPropertyOrderResult, query: {
      'workOrderId': orderId.toString(),
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return OrderResultEntity().copyWith(
        code: ResponseCode.timeout,
        obj: OrderResultObj().copyWith(
            fatalType: "",
            standard: '',
            result: '',
            cause: '',
            propose: '',
            appraise: '',
            pics: [],
            cost: -1,
            assistants: ''),
      );
    }
    OrderResultEntity entity = OrderResultEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<RepairDepartmentListEntity> getRepairDepartmentList() async {
    var response = await get(Api.getRepairDepartmentList);
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value(
        RepairDepartmentListEntity()
            .copyWith(code: ResponseCode.timeout, obj: [], msg: ''),
      );
    }
    RepairDepartmentListEntity entity =
        RepairDepartmentListEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<RepairStaffListEntity> getRepairStaffList({int? did}) async {
    var response = await get(Api.getRepairStaffList, query: {
      'did': did.toString(),
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value(
        RepairStaffListEntity()
            .copyWith(code: ResponseCode.timeout, obj: [], msg: ''),
      );
    }
    RepairStaffListEntity entity =
        RepairStaffListEntity.fromJson(response.body);
    return Future.value(entity);
  }
}
